/**
 * 表单数据模型
 */
module.exports = (app) => {
  const { INTEGER, STRING } = app.Sequelize
  const DataSource = app.model.define(
    'data_source',
    {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      code: STRING(20),
      name: STRING(50),
      hickeyId: {
        type: INTEGER,
        allowNull: false,
      },
    },

    {
      timestamps: false,
    }
  )

  DataSource.associate = function () {
    app.model.DataSource.belongsToMany(app.model.Parameter, {
      through: app.model.DataSourceParameter,
      foreignKey: 'dataSourceId',
      otherKey: 'parameterId',
      as: 'parameters',
    })

    app.model.DataSource.belongsToMany(app.model.Return, {
      through: app.model.DataSourceReturn,
      foreignKey: 'dataSourceId',
      otherKey: 'returnId',
      as: 'returns',
    })
  }

  return DataSource
}
